<?php defined('BASEPATH') || exit('No direct script access allowed');

class Photos_model extends BF_Model
{
    protected $table_name	= 'photos';
	protected $key			= 'id';
	protected $date_format	= 'datetime';

	protected $log_user 	= false;
    protected $set_created	= false;//true;
	protected $set_modified = false;//true;
	protected $soft_deletes	= true;

	protected $created_field     = 'created_on';
	protected $modified_field    = 'modified_on';
    protected $deleted_field     = 'deleted';

	// Customize the operations of the model without recreating the insert,
    // update, etc. methods by adding the method names to act as callbacks here.
	protected $before_insert 	= array();
	protected $after_insert 	= array();
	protected $before_update 	= array();
	protected $after_update 	= array();
	protected $before_find 	    = array();
	protected $after_find 		= array('set_photo_domain');
	protected $before_delete 	= array();
	protected $after_delete 	= array();

	// For performance reasons, you may require your model to NOT return the id
	// of the last inserted row as it is a bit of a slow method. This is
    // primarily helpful when running big loops over data.
	protected $return_insert_id = true;

	// The default type for returned row data.
	protected $return_type = 'object';

	// Items that are always removed from data prior to inserts or updates.
	protected $protected_attributes = array();

	// You may need to move certain rules (like required) into the
	// $insert_validation_rules array and out of the standard validation array.
	// That way it is only required during inserts, not updates which may only
	// be updating a portion of the data.
	protected $validation_rules 		= array(
	
	);
	protected $insert_validation_rules  = array();
	protected $skip_validation 			= false;

    /**
     * Constructor
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function prep_data($post_data) {
        $data = parent::prep_data($post_data);

        $url = $post_data['url'];
        if (is_array($url)) {
            $title = $post_data['title'];
            $desc = isset($post_data['desc']) ? $post_data['desc'] : null;
            $target_id = $post_data['target_id'];
            $platform = $post_data['platform'];
            // 整理新增数据
            $data = [];
            foreach ($url as $key => $val) {
                $data[] = array('url'=>$val, 'title'=>$title[$key], 'desc'=>$desc ? $desc[$key] : '', 'target_id'=>$target_id, 'platform'=>$platform[$key], 'type'=>$post_data['type'], 'sub_type'=>$post_data['sub_type']);
            }
        }

        return $data;
    }

    /**
     * 设置图片域名
     * @param object $row
     */
    protected function set_photo_domain($row) {
        if (is_object($row)) {
            $domain = is_fang90_server_name() ? config_item('photo.fang90.domain') : config_item('photo.domain');
            $row->absolute_url = $domain.$row->url;
            if (is_plusfang_server_name()) {
                $row->absolute_url = config_item('photo.plusfang.domain').$row->url;
            }
            if (is_pinxiaochina_server_name()) {
                $row->absolute_url = config_item('photo.pinxiaochina.domain').$row->url;
            }
        }
        return $row;
    }

    /**
     * 获取图片
     * @param  int $deleted
     * @return mixed            数组或者false或者空
     */
    public function photosBy($targetId, $targetSubId = 0, $type = 1, $subType = 1, $deleted = 0) {
    	$this->db->where(array('target_id'=>$targetId, 'target_sub_id'=>$targetSubId, 'type'=>$type, 'sub_type'=>$subType, 'deleted'=>$deleted));
    	return $this->find_all();
    }

    /**
     * 获取楼盘图片
     * @param  int $deleted
     * @return mixed            数组或者false或者空
     */
    public function getPhotosByBuildingId($buildingId, $type = 1) {
        $deleted = 0;
        $this->where(array('target_id'=>$buildingId, 'type'=>$type, 'deleted'=>$deleted))->order_by('sub_type');
        return $this->find_all();
    }
   /**
     * 获取楼盘户型图片
     * @param  int $deleted
     * @return mixed            数组或者false或者空
     */
    public function getHuXingPhotosBy($buildingId, $targetSubId = 0,$type = 1) {
        $deleted = 0;
        $this->where(array('target_id'=>$buildingId, 'target_sub_id'=>$targetSubId, 'type'=>$type, 'deleted'=>$deleted))->order_by('sub_type');
        return $this->find_all();
    }
    /**
     * 添加多个图片
     *
     * @param array $data 数据
     * @return mixed 成功返回添加的条数，否则返回false
     */
    public function addPhotos($data) {
        $return = false;
        if (is_array($data)) {
            $return = $this->db->insert_batch($this->table_name, $data);
        }
        return $return;
    }

    /**
     * 添加多个户型图片
     * @param int $buildingId 楼盘ID
     * @param array $urlArr     图片地址数组
     */
    public function addHuxingPhotos($buildingId, $hxId, $urlArr,$platformArr) {
        $return = false;
        if (is_array($urlArr)) {
            $where = array('target_id'=>$buildingId, 'target_sub_id'=>$hxId, 'type'=>1, 'sub_type'=>4);
            // 删除已有的数据
            parent::delete_where($where);
            // 新增数据
            foreach ($urlArr as $key => $val) {
                $where['url'] = $val;
                $where['platform'] = $platformArr[$key];
                $data[] = $where;
            }
            $return = $this->db->insert_batch($this->table_name, $data);
        }
        return $return;
    }
     /**
     * 获取楼盘封面图片
     * @param  int $deleted
     * @return mixed            数组或者false或者空
     */
    public function getCoverPhotoByBuildingId($buildingId,$platform = '1') {
        $deleted = 0;
       $where = array(
                    'target_id'=>$buildingId,
                    'platform'=>$platform,
                     'type'=>1,
                     'sub_type'=>1, 
                     'deleted'=>$deleted
                     );
        return $this->where($where)->find_by($where);
    }
}